package com.greyshark.bledemo.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


abstract class SimpleAdapter<T, VB : ViewBinding>(
    open var data: MutableList<T>,
    open val inflater: (
        LayoutInflater,
        parent: ViewGroup,
        attachToRoot: Boolean
    ) -> VB
) : RecyclerView.Adapter<ViewHolder<VB>>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder<VB> {
        return ViewHolder(
            inflater(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(
        holder: ViewHolder<VB>,
        position: Int
    ) {
        onBindData(holder.t, data[position], position);
    }

    override fun getItemCount(): Int {
        return data.size
    }

    protected abstract fun onBindData(
        binding: VB?,
        t: T,
        position: Int
    )

    override fun getItemViewType(
        position: Int
    ): Int {
        return 0
    }
}